﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Facebook_Graph_Toolkit {
    /// <summary>
    /// Stores Facebook Application settings.
    /// </summary>
    public class FacebookAppConfig{

        /// <summary>
        /// Facebook Application ID
        /// </summary>
        public string FacebookAppID;
        /// <summary>
        /// Facebook Application Secret
        /// </summary>
        public string FacebookAppSecret;
        /// <summary>
        /// Facebook Application Canvas Address (in the form of http://apps.facebook.com/xxx )
        /// </summary>
        public string FacebookAppAddress;
        /// <summary>
        /// The redirect url relative to the Canvas Address after authorization
        /// </summary>
        public string PostAuthorizeRedirectURL;

        /// <summary>
        /// Initialize a new instance of FacebookAppConfig
        /// </summary>
        public FacebookAppConfig() {
            FacebookAppID = null;
            FacebookAppSecret = null;
            FacebookAppAddress = null;
            PostAuthorizeRedirectURL = null;
        }

        /// <summary>
        /// Obtain Facebook Application Settings from web.config if settings are unspecified
        /// </summary>
        public void GetDefaultConfig() {
            if (string.IsNullOrEmpty(FacebookAppAddress) || string.IsNullOrEmpty(FacebookAppID) || string.IsNullOrEmpty(FacebookAppSecret) || string.IsNullOrEmpty(PostAuthorizeRedirectURL)) {
                FacebookGraphToolkitConfiguration Config = Helpers.Common.GetAppConfig(true);
                if (string.IsNullOrEmpty(FacebookAppAddress)) FacebookAppAddress = Config._FacebookAppAddress;
                if (string.IsNullOrEmpty(FacebookAppID)) FacebookAppID = Config._FacebookAppID;
                if (string.IsNullOrEmpty(FacebookAppSecret)) FacebookAppSecret = Config._FacebookAppSecret;
                if (string.IsNullOrEmpty(PostAuthorizeRedirectURL)) PostAuthorizeRedirectURL = Config._PostAuthorizeRedirectURL;
            }
            if (string.IsNullOrEmpty(FacebookAppID)) throw new FacebookException("Facebook Application Configuration missing: FacebookAppID");
            if (string.IsNullOrEmpty(FacebookAppSecret)) throw new FacebookException("Facebook Application Configuration missing: FacebookAppSecret");
            if(!string.IsNullOrEmpty(FacebookAppAddress)) if (!FacebookAppAddress.EndsWith("/")) throw new FacebookException("FacebookAppAddress must end with a closing slash (\"/\")");
        }
    }
}
